﻿using EA.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EA.DAO
{
    public class AuctionLogDA : ObjectDA
    {
        public AuctionLogDA(String connection_string)
            : base(connection_string)
        {

        }

        public DataTable GetAllAuctionLogByProductID(int productID)
        {
            String query = "Select a.*, c.Email From AuctionLog a inner join customer c on c.ID=a.buyerID and ProductID=@productID";
            SqlParameter[] param=new SqlParameter[1];
            param[0] = new SqlParameter("productID", productID);
            DataTable table = new DataTable();
            this.Fill(query,param,table);
            return table;
        }

        public DataTable GetEndAuctionLogByProductID(int productID)
        {
            String query = "Select TOP 1 a.*, c.Email From AuctionLog a inner join customer c on c.ID=a.buyerID and ProductID=@productID ORDER BY ID DESC";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("productID", productID);
            DataTable table = new DataTable();
            this.Fill(query, param, table);
            return table;
        }

        public Boolean InsertAuctionLog(AuctionLogTO auctionLogTO)
        {
            String query = "INSERT INTO AuctionLog(ProductID,BuyerID,BidDate,Price,Status) VALUES(@ProductID,@BuyerID,@BidDate,@Price,@Status)";
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@ProductID", auctionLogTO.ProductID);
            param[1] = new SqlParameter("@BuyerID", auctionLogTO.BuyerID);
            param[2] = new SqlParameter("@BidDate",auctionLogTO.BidDate);
            param[3] = new SqlParameter("@Price",auctionLogTO.Price);
            param[4] = new SqlParameter("@Status", auctionLogTO.Status);
            return this.ExecuteNonQuery(query, param);
        }
    }
}
